package src.lab03.test14;

import java.sql.*;

/**
 * Created by 阿阿阿阿豪丶 on 2017/12/15.
 */
public class Test {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        String insert ="insert INTO "
                + "t_userinfo(usercode,username,password,email)"
                +"values ('7106','曲小声','654321','xiaosheng@263.net')";
        String select = "select * from t_userinfo";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",
                    "root",
                    "123456"
            );
            statement = connection.createStatement();
//            statement.execute(insert);
            resultSet = statement.executeQuery(select);
            System.out.println("原来的数据是：");
            while (resultSet.next()){

                System.out.print(resultSet.getString("usercode")+"  ");
                System.out.print(resultSet.getString("username")+"  ");
                System.out.print(resultSet.getString("password")+"  ");
                System.out.println(resultSet.getString("email")+"   ");
            }

            statement.execute(insert);
            resultSet = statement.executeQuery(select);

            while (resultSet.next()){
                System.out.println();
                System.out.print(resultSet.getString("usercode")+"  ");
                System.out.print(resultSet.getString("username")+"  ");
                System.out.print(resultSet.getString("password")+"  ");
                System.out.println(resultSet.getString("email")+"   ");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
